package com.cskaoyan.config;


import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.PeriMissionListMapper;
import com.cskaoyan.mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期: 2022/05/09 16:12
 *
 * @author 孙豪骏
 */
@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    PeriMissionListMapper periMissionListMapper;
    @Autowired
    UserMapper userMapper;


    //该方法的形参来源于
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {


        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;


        //这上面的token是从authorcontrol的界面传入的
        //这一行是根据传入的Usertoken里的 User查找一个admin list 不能重复，所以肯定只能找到一个或者没有
        List<Admin> adminListByLogUserName = getByUsername(token);
        //如果这个查询的结果为空,直接返回一个空。

        if (adminListByLogUserName == null || adminListByLogUserName.size() == 0) {
            UserExample userExample = new UserExample();
            UserExample.Criteria criteria = userExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            criteria.andUsernameEqualTo(token.getUsername());
            List<User> userList = userMapper.selectByExample(userExample);
            if (userList.size() == 0) {
                return null;
            }else {
                return new SimpleAuthenticationInfo(userList.get(0).getId(), userList.get(0).getPassword(), getName());
            }
        }
        //todo:这里要写一个抛出异常然后把他解决的东西
        //如果这个查询的结果不为空
        Admin admin = adminListByLogUserName.get(0);
        //这个下面的getName老师是这么写的目的是传入这个realmDe的名字
        //这里据说可以放一个user进去，试一试
        return new SimpleAuthenticationInfo(admin, admin.getPassword(), getName());

    }
//这个是上面根据username查询的方法


    private List<Admin> getByUsername(UsernamePasswordToken token) {
        String username = token.getUsername();
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andDeletedEqualTo(false);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        return admins;
    }

    
    @Override
    // 根据用户信息拿到所有权限
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Admin admin = (Admin) principalCollection.getPrimaryPrincipal();
        List<String> permissions = new ArrayList<>();
        Integer[] roleIds = admin.getRoleIds();
        for (int i = 0; i < roleIds.length; i++) {
            PeriMissionListExample periMissionListExample = new PeriMissionListExample();
            PeriMissionListExample.Criteria criteria = periMissionListExample.createCriteria();
            criteria.andRoleIdEqualTo(roleIds[i]);
            List<String> periMissionLists = periMissionListMapper.selectByExample(periMissionListExample);
            permissions.addAll(periMissionLists);
        }
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permissions);
        return null;
    }

}

